/*
 * Created by JFormDesigner on Wed Nov 30 11:20:27 EET 2011
 */

package tr.gov.tubitak.bilgem.esya.certviewer;

import tr.gov.tubitak.bilgem.esya.certviewer.viwerasn.ECertViewerCertificate;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.security.cert.CertificateParsingException;
import java.security.cert.X509Certificate;

/**
 * @author Haydar Karabas
 */
public class ECertViewer extends JPanel {
    String certPath;
    public ECertViewer()
    {
        initComponents();
    }
    public ECertViewer(String certPath) {
        initComponents();
        this.certPath = certPath;
        loadCert(certPath);
    }

    public void addTab(String tabTitle,JPanel tabPage)
    {
        eCertInfoTabPane.add(tabTitle,tabPage);
    }

    public void loadCert(X509Certificate x509Certificate)
    {
        try
        {

            ECertViewerCertificate eCertViewerCertificate= new ECertViewerCertificate(x509Certificate);
            boolean  isQualified = eCertViewerCertificate.isQualifiedCertficate();


            ECertGeneralViewPanel generalViewPanel = new ECertGeneralViewPanel(x509Certificate);
            eCertInfoTabPane.add(ECertGeneralViewPanel.getTitle(),generalViewPanel);

            ECertDetailViewPanel detailViewPanel = new ECertDetailViewPanel(x509Certificate);
            eCertInfoTabPane.add(ECertDetailViewPanel.getTitle(),detailViewPanel);
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null,e.getMessage());
          //  setVisible(false);
        }
    }

    public void loadCert(byte [] certData){

        try
        {
            X509Certificate x509Certificate = ECertUtil.loadX509Certificate(certData);
            loadCert(x509Certificate);
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null,e.getMessage());
            setVisible(false);
        }

    }
    public void loadCert(String certPath) {
        //To change body of created methods use File | Settings | File Templates.
        eCertInfoTabPane.removeAll();
        byte [] asnData=null;
        try {
            asnData =ECertUtil.getDERCertDataFromFile(new File(certPath));

        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            JOptionPane.showMessageDialog(null,e.getMessage());
        } catch (CertificateParsingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            JOptionPane.showMessageDialog(null,e.getMessage());
        }
        if(asnData!=null)
        {
            loadCert(asnData);
        }
    }

    private void eCertViewerButton1ActionPerformed(ActionEvent e) {
        getParent().getParent().setVisible(false);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        eCertInfoTabPane = new ECertInfoTabPane();
        panel1 = new JPanel();

        //======== this ========
        setLayout(new BorderLayout());
        add(eCertInfoTabPane, BorderLayout.CENTER);

        //======== panel1 ========
        {
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
        }
        add(panel1, BorderLayout.PAGE_END);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private ECertInfoTabPane eCertInfoTabPane;
    private JPanel panel1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
